export default class label {
  constructor(love, conf) {
    this.love = love
    this.text = conf.text || "default"
    this.x = conf.x || 0
    this.y = conf.y || 0
    this.w = conf.w || 100
    this.h = conf.h || 40
    this.font = conf.font || love.ui.font
    this.textColor = conf.textColor || love.ui.textColor
    this.visible = true
    this.onTouch = conf.onTouch ? conf.onTouch.bind(this) : function () { }
    this.onHold = conf.onHold ? conf.onHold.bind(this) : function () { }
    this.holdTimer = 0
    this.holdTime = conf.holdTime || 2000
    this.available = true
  }
  check(x, y) {
    return this.x < x && this.x + this.w > x && this.y < y && this.y + this.h > y
  }
  checkTouch(x, y) {
    let rt = this.check(x, y)
    if (rt) {
      this.onTouch()
    }
    return rt
  }
  checkHold(x, y, dt) {
    let rt = this.check(x, y)
    if (rt) {
      this.holding = true
      this.holdTimer += dt
      if (this.holdTimer > this.holdTime) {
        this.onHold()
        return true
      }
    } else {
      this.holding = false
      this.holdTimer = 0
    }
  }
  draw() {
    this.love.graphics.setColor(this.textColor)
    let fontW = this.font.getWidth(this.text)
    let fontH = this.font.getHeight()
    this.love.graphics.setFont(this.font)
    this.love.graphics.print(this.text, this.x + (this.w - fontW) / 2, this.y + this.h / 2 + fontH / 2 - (this.frameWidth || 0), this.w)
  }
}